package Code.Final.StudentManagementSystem;

import javax.swing.plaf.PanelUI;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
import java.util.Stack;

public class  LoginInterface {

    private static final String LOGIN = "1";
    private static final String ENROLL = "2";
    private static final String FORGET = "3";
    private static final String EXIT = "4";

    static ArrayList<User> list = new ArrayList<>(); //创建用户类集合

    static {
        list.add(new User("w123", "123", "42", "123"));
    }
    public static void main(String[] args) {

        System.out.println("----------欢-迎-来-到-学-生-管-理-系-统----------");
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.print("请选择操作1登录 2注册 3忘记密码 4退出：");
            String choose = scanner.next();
            switch (choose) {
                case LOGIN -> LogIn(list);
                case ENROLL -> Enroll(list);
                case FORGET -> Forget(list);
                case EXIT -> {
                    System.out.println("退出成功！");
                    System.exit(0); //结束虚拟机的运行
                }
                default -> {
                    System.out.println("没有这个选项");
                }
            }

        }
    }

    //登录
    public static void LogIn(ArrayList<User> list) {
        System.out.print("请输入用户名：");
        String userNmae = StudentSystem.Print();
        System.out.print("请输入密码：");
        String password = StudentSystem.Print();
        if (!find(list, userNmae)) {
            System.out.println("用户名未注册，请先注册");
            return;
        }
        //判断验证码
        while (true) {
            String str = VerificationCode();  //验证码
            System.out.print("请输入验证码(" + str + ")：");
            String verifyNumber = StudentSystem.Print();
            if (verifyNumber.equalsIgnoreCase(str)) {
                break;
            } else {
                System.out.println("验证码错误，请重新输入");
            }
        }
        //判断密码
        //通过用户名来获取它在集合里面的密码
        int index = userIndex(list, userNmae);
        String uesrPassword = list.get(index).getPassword();
        for (int i = 0; i < 3; i++) {
            if (password.equals(uesrPassword)) {
                System.out.println("登录成功！");
                StudentSystem.StartStudentSystem();//跳转到学生管理系统
            } else {
                if (i == 2) {
                    System.out.println("登录失败,请重新操作");
                    return;
                } else {
                    System.out.print("密码错误，请重新输入(还有" + (2 - i) + "机会)：");
                    password = StudentSystem.Print();
                }
            }
        }


    }

    //注册
    public static void Enroll(ArrayList<User> list) {
        System.out.println("注册");
        User user = new User();  //创建User类
        //输入用户名
        while (true) {
            System.out.print("请输入用户名：");
            String userName = StudentSystem.Print();
            user.setUserName(userName);  //调用StudentClass的输入方法
            if (user.getUserName() != null) { //判单用户名格式是否正确
                if (!find(list, userName)) { //判断用户名是否唯一
                    break;
                } else {
                    System.out.println("用户名" + userName + "已存在，请重新输入");
                }
            }
        }
        //输入密码
        while (true) {
            System.out.print("请输入密码：");
            String password = StudentSystem.Print();
            System.out.print("请再次输入密码：");
            String newPassword = StudentSystem.Print();
            if (password.equals(newPassword)) {
                user.setPassword(password);
                break;
            } else {
                System.out.println("两次密码不一致，请重新输入");
                continue;
            }
        }
        //输入身份证号码
        while (true) {
            System.out.print("请输入身份证号：");
            user.setIdentityCard(StudentSystem.Print());
            if (user.getIdentityCard() != null) {
                break;
            }
        }
        //输入手机号
        while (true) {
            System.out.print("请输入手机号：");
            user.setPhoneNumber(StudentSystem.Print());
            if (user.getPhoneNumber() != null) {
                break;
            }
        }
        list.add(user);//添加集合元素
        System.out.println("注册成功！");
        System.out.println(user.getUserName() + " " + user.getPassword() + " " + user.getIdentityCard() + " " + user.getPhoneNumber());
    }

    //忘记密码
    public static void Forget(ArrayList<User> list) {
        System.out.print("请输入用户名：");
        String userName = StudentSystem.Print();
        if (!find(list, userName)) {
            System.out.println("未注册");
            return;
        }
        System.out.print("请输入身份证号码：");
        String identity = StudentSystem.Print();
        System.out.print("请输入手机号码：");
        String phoneNumber = StudentSystem.Print();
        //获取用户名在集合中的身份证和电话号码
        int index = userIndex(list, userName);
        User u = list.get(index);
        if (!(identity.equals(u.getIdentityCard()) && phoneNumber.equals(u.getPhoneNumber()))) {
            System.out.println("账号信息不匹配，修改失败");
            return;
        }
        while (true){
            System.out.print("请输入修改的密码：");
            String password = StudentSystem.Print();
            System.out.println("请再次输入修改的密码");
            String newPassword = StudentSystem.Print();
            if (password.equals(newPassword)) {
                u.setPassword(password);
                System.out.println("修改成功！");
                break;
            }else {
                System.out.println("两次密码不一致，请重新输入");
            }
        }
    }

    //查找用户名是否存在
    public static boolean find(ArrayList<User> list, String name) {
        return userIndex(list, name) >= 0;
    }

    //通过用户名来找索引
    public static int userIndex(ArrayList<User> list, String name) {
        for (int i = 0; i < list.size(); i++) {  //遍历集合的每个元素
            String studentID = list.get(i).getUserName();  //获取元素的用户名
            if (studentID.equals(name)) {  //拿元素的用户名和输入的用户进行对比
                return i;  //存在返回索引
            }
        }
        return -1;  //不存在返回-1
    }

    //生成验证码
    public static String VerificationCode() {
        char[] arr = new char[52];
        //往数组里面添加大小写字母
        for (int i = 0; i < arr.length; i++) {
            if (i < 26) {
                arr[i] = (char) ('a' + i);
            } else {
                arr[i] = (char) ('A' + i - 26);
            }
        }
        //创建验证码数组
        char[] verifyArr = new char[5];
        Random r = new Random();
        for (int i = 0; i < verifyArr.length - 1; i++) {
            int index = r.nextInt(arr.length);
            verifyArr[i] = arr[index];
        }
        int end = verifyArr.length - 1; //最后一位索引
        verifyArr[end] = (char) ('0' + r.nextInt(10));

        //将数字的索引位置打乱
        int index = r.nextInt(verifyArr.length);
        char temp = verifyArr[index]; //临时保存随机索引位置的元素
        verifyArr[index] = verifyArr[end]; //将最后一位元素放在索引位置上
        verifyArr[end] = temp;

        return new String(verifyArr);
    }

}
